package com.fancy.learncenter.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.ImageUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.utils.WXShareUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.util.Hashtable;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CartoonShareClassActivity extends BaseActivity {


    @Bind(R.id.class_num)
    TextView classNum;
    @Bind(R.id.teacher_name)
    TextView teacherName;
    @Bind(R.id.share_img)
    ImageView shareImg;
    @Bind(R.id.share_ll)
    RelativeLayout shareLl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_share_class, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("创建成功");

        String classNo = getIntent().getStringExtra("classNo");
        classNum.setText("【" + classNo + "】加入讨论吧～");
        teacherName.setText("英语老师：" + LoginUserSharePrefernce.getName());
        String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.fancy.learncenter";
        createQRImage(url, shareImg);
    }

    // 要转换的地址或字符串,可以是中文
    public void createQRImage(String fid, ImageView imageView) {
        int QR_WIDTH = Utils.dip2pix(this, 300);
        int QR_HEIGHT = Utils.dip2pix(this, 300);
        try {
            // 判断URL合法性
            if (fid == null || "".equals(fid) || fid.length() < 1) {
                return;
            }
            Hashtable hints = new Hashtable();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            // 图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(fid,
                    BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            // 下面这里按照二维码的算法，逐个生成二维码的图片，
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }
            // 生成二维码图片的格式，使用ARGB_8888
            bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
                    Bitmap.Config.ARGB_8888);

            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            // 显示到一个ImageView上面

            imageView.setImageBitmap(bitmap);

        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    Bitmap bitmap;


    @OnClick({R.id.weixin_text, R.id.friends_circle_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.weixin_text:
                WXShareUtil.getInstance(this).sharePicBitmap(ImageUtil.loadBitmapFromView(shareLl), false);
                break;
            case R.id.friends_circle_text:
                WXShareUtil.getInstance(this).sharePicBitmap(ImageUtil.loadBitmapFromView(shareLl), true);
                break;
        }
    }
}
